package cn.hospital.controller;

import cn.hospital.entity.ClinicalDataQuery;
import cn.hospital.entity.MeaningUser;
import cn.hospital.entity.QueryAuditManagement;
import cn.hospital.service.ClinicalDataQueryService;
import cn.hospital.service.DropDownBoxQueryService;
import cn.hospital.service.MeaningUserService;
import cn.hospital.service.PatientInformationSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ClinicalDataQuery")
public class ClinicalDataQueryController {
    @Autowired
    private ClinicalDataQueryService clinicalDataQueryService;
    @Resource
    private DropDownBoxQueryService dropDownBoxQueryService;
    @Autowired
    private PatientInformationSheetService patientInformationSheetService;
    @Autowired
    private MeaningUserService meaningUserService;

    /**
     * 查询，有效的申请数据查询
     * @param pageNoStr   页码
     * @param pageSizeStr 每一页面展示的条数
     * @return
     */
    @RequestMapping("/findByLessTime")
    @ResponseBody
    public Map<String, Object> findByLessTime(String pageNoStr, String pageSizeStr) {
        Date time = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> mapList = new HashMap<String, Object>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        /*条件map集合*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("time", dateFormat.format(time));
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("clinicalDataQueryList", clinicalDataQueryService.findByEffective(map));
        mapList.put("countList", clinicalDataQueryService.findByCountEffective(map));
        return mapList;
    }

    /**
     * 查询，无效的申请数据查询
     * @param pageNoStr   页码
     * @param pageSizeStr 每一页面展示的条数
     * @return
     */
    @RequestMapping("/findByGreaterTime")
    @ResponseBody
    public Map<String, Object> findByGreaterTime(String pageNoStr, String pageSizeStr) {
        Date time = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> mapList = new HashMap<String, Object>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        /*条件map集合*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("time", dateFormat.format(time));
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("countList", clinicalDataQueryService.findByCountInvalid(map));
        mapList.put("clinicalDataQueryList", clinicalDataQueryService.findByInvalid(map));
        return mapList;
    }

    /**
     * 查询，有效的申请数据查询中正在审批中
     * @param pageNoStr   页码
     * @param pageSizeStr 每一页面展示的条数
     * @return
     */
    @RequestMapping("/findByLessTimeInExamination")
    @ResponseBody
    public Map<String, Object> findByLessTimeInExamination(String pageNoStr, String pageSizeStr) {
        Date time = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> mapList = new HashMap<String, Object>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        /*条件map集合*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("time", dateFormat.format(time));
        map.put("approverState", 1);
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("countList", clinicalDataQueryService.findByCountEffectiveInExamination(map));
        mapList.put("clinicalDataQueryList", clinicalDataQueryService.findByEffective(map));
        return mapList;
    }

    /**
     * 删除查询信息
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("/deleteBy")
    public String deleteBy(String queryConditionsId) {
        Integer count = clinicalDataQueryService.deleteBy(queryConditionsId);
        return "redirect:/page/ClinicalDataQuerySystem/clinicalDataQueryDisplay.jsp";
    }

    /**
     * 根据查询信息id，查询该条查询信息
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("updateByQueryConditionsId")
    public String updateByQueryConditionsId(String queryConditionsId, HttpSession session) {
        ClinicalDataQuery clinicalDataQuery = clinicalDataQueryService.updateByQueryConditionsId(queryConditionsId);
        session.setAttribute("clinicalDataQuery", clinicalDataQuery);
        return "redirect:/page/ClinicalDataQuerySystem/updateEndTime.jsp";
    }

    /**
     * 修改有效时间，根据查询信息id修改有效时间
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("/updateByEndTime")
    @ResponseBody
    public Map<String, Object> updateByEndTime(String queryConditionsId, String updateEndtime) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", clinicalDataQueryService.updateByEndTime(queryConditionsId, updateEndtime));
        return map;
    }

    /**
     * 查询审批过程
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("/selectApproval")
    public String selectApproval(String queryConditionsId, HttpSession session) {
        session.setAttribute("meaningUserList", dropDownBoxQueryService.selectByMeaningUser());
        session.setAttribute("clinicalDataQuery", clinicalDataQueryService.updateByQueryConditionsId(queryConditionsId));
        session.setAttribute("queryAuditManagementList", clinicalDataQueryService.selectByClinicalDataQueryId(queryConditionsId));
        return "redirect:/page/ClinicalDataQuerySystem/resultsOfExaminationAndApproval.jsp";
    }

    /**
     * 查询单个查询信息的详细方法
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("/selectByClinicalDataQuery")
    public String selectByClinicalDataQuery(String queryConditionsId, HttpSession session) {
        session.setAttribute("grouptableList", dropDownBoxQueryService.findByGrouptable());
        session.setAttribute("departmentList", dropDownBoxQueryService.findByhospitalId(session));
        session.setAttribute("clinicalDataQuery", clinicalDataQueryService.updateByQueryConditionsId(queryConditionsId));
        session.setAttribute("QueryConditionsList", clinicalDataQueryService.selectByApplicationInformationId(queryConditionsId));
        session.setAttribute("count", patientInformationSheetService.selectByMapCount(queryConditionsId));
        return "redirect:/page/ClinicalDataQuerySystem/detailsNewQuery.jsp";
    }

    /**
     * 查询单个查询管理信息的详细方法
     *
     * @param queryConditionsId
     * @return
     */
    @RequestMapping("/selectMangagementByClinicalDataQuery")
    public String selectMangagementByClinicalDataQuery(String queryConditionsId, HttpSession session) {
        session.setAttribute("grouptableList", dropDownBoxQueryService.findByGrouptable());
        session.setAttribute("departmentList", dropDownBoxQueryService.findByhospitalId(session));
        session.setAttribute("clinicalDataQuery", clinicalDataQueryService.updateByQueryConditionsId(queryConditionsId));
        session.setAttribute("QueryConditionsList", clinicalDataQueryService.selectByApplicationInformationId(queryConditionsId));
        session.setAttribute("count", patientInformationSheetService.selectByMapCount(queryConditionsId));
        return "redirect:/page/ClinicalDataQuerySystem/detailedAuditManagement.jsp";
    }

    /*-----------------------------------------(审核管理)------------------------------------------*/

    /**
     * 审核查询信息，待审核
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectByAuditEffective")
    public Map<String, Object> selectByAuditEffective(HttpSession session, String pageNoStr, String pageSizeStr) {
        Map<String, Object> mapList = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("ClinicalDataQueryList", clinicalDataQueryService.selectByAuditEffective(session, map));
        mapList.put("countList", clinicalDataQueryService.selectByAuditEffectiveCount(session, map));
        mapList.put("MeaningUserList", clinicalDataQueryService.selectMeaningUserBy());
        return mapList;
    }

    /**
     * 审核查询信息，审核通过
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectByApprovedEffective")
    public Map<String, Object> selectByApprovedEffective(HttpSession session, String pageNoStr, String pageSizeStr) {
        Map<String, Object> mapList = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("ClinicalDataQueryList", clinicalDataQueryService.selectByApprovedEffective(session, map));
        mapList.put("countList", clinicalDataQueryService.selectByApprovedEffectiveCount(session, map));
        mapList.put("MeaningUserList", clinicalDataQueryService.selectMeaningUserBy());
        return mapList;
    }

    /**
     * 审核查询信息，审核未通过
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectByFailedPassAuditEffective")
    public Map<String, Object> selectByFailedPassAuditEffective(HttpSession session, String pageNoStr, String pageSizeStr) {
        Map<String, Object> mapList = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        mapList.put("ClinicalDataQueryList", clinicalDataQueryService.selectByFailedPassAuditEffective(session, map));
        mapList.put("countList", clinicalDataQueryService.selectByFailedPassAuditEffectiveCount(session, map));
        mapList.put("MeaningUserList", clinicalDataQueryService.selectMeaningUserBy());
        return mapList;
    }

    /**
     * 跳转页面到审核而修改状态页面
     *
     * @param queryAuditManagementId
     * @param session
     * @return
     */
    @RequestMapping("/AuditChanges")
    public String AuditChanges(Integer queryAuditManagementId, String clinicalDataQueryId, HttpSession session) {
        session.setAttribute("clinicalDataQueryId", clinicalDataQueryId);
        session.setAttribute("queryAuditManagementId", queryAuditManagementId);
        return "redirect:/page/ClinicalDataQuerySystem/AuditChanges.jsp";
    }

    /**
     * 审核方法
     *
     * @param queryAuditManagement
     * @return
     */
    @RequestMapping("/ModifyAuditChanges")
    public String ModifyAuditChanges(QueryAuditManagement queryAuditManagement, HttpSession session) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        queryAuditManagement.setQueryAuditManagementTime(new Date());
        queryAuditManagement.setQueryAuditManagementPeople(meaningUser.getUserId());
        Integer count = clinicalDataQueryService.updateByqueryAuditManagementId(queryAuditManagement);
        return "redirect:/page/ClinicalDataQuerySystem/auditManagement.jsp";
    }

    /*------------------------------下面是新增按钮里面的操作--------------------------------*/
    /**
     * 添加我的查询信息
     * @param clinicalDataQuery
     * @return
     */
    @RequestMapping("/addClinicalDataQuery")
    @ResponseBody
    public Map<String, Object> addClinicalDataQuery(ClinicalDataQuery clinicalDataQuery,
                                                    HttpSession session, String[] exportWays)
            throws ParseException {
        /*导出方式*/
        String exportWay = "";
        for (int i = 0; i < exportWays.length; i++) {
            exportWay += exportWays[i] + ",";
        }
        clinicalDataQuery.setExportWay(exportWay);
        /*结束日期转换问题*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        clinicalDataQuery.setEndTime(sdf.parse(clinicalDataQuery.getEndTimes()));
        Map<String, Object> map = new HashMap<String, Object>();
        session.setAttribute("clinicalDataQuery", clinicalDataQuery);
        return map;
    }

    /**
     * 添加查询条件
     * @param query_field
     * @param query
     * @param query_conditions
     * @param scope_of
     * @return
     */
    @RequestMapping("/addQueryConditions")
    @ResponseBody
    public Map<String, Object> addQueryConditions(String[] query_field, String[] query,
                                                  String[] query_conditions, String[] scope_of,
                                                  HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        session.setAttribute("query_field", query_field);
        session.setAttribute("query", query);
        session.setAttribute("query_conditions", query_conditions);
        session.setAttribute("scope_of", scope_of);
        return map;
    }

    /**
     * 根据查询条件，查询病人数量
     * @return
     */
    @RequestMapping("/selectBy")
    @ResponseBody
    public Map<String, Object> selectBy(Map<String, Object> map, HttpSession session) {
        Map<String, Object> mapList = new HashMap<String, Object>();
        String[] query_field = (String[]) session.getAttribute("query_field");
        String[] query = (String[]) session.getAttribute("query");
        String[] query_conditions = (String[]) session.getAttribute("query_conditions");
        String[] scope_of = (String[]) session.getAttribute("scope_of");
        mapList.put("count", patientInformationSheetService.selectByMapCount(map, session));
        mapList.put("query_field", query_field);
        mapList.put("query", query);
        mapList.put("query_conditions", query_conditions);
        mapList.put("scope_of", scope_of);
        return mapList;
    }

    /**
     * 点击结果项中的保存按钮执行该方法，实现查询的保存
     * @return
     */
    @RequestMapping("/addBy")
    @ResponseBody
    public Map<String, Object> addBy(HttpSession session) {
        Map<String, Object> mapList = new HashMap<String, Object>();
        mapList.put("count", clinicalDataQueryService.addBy(session));
        return mapList;
    }


}
